﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using PreBuildGrupyz.Controllers;
using PreBuildGrupyz.Routes;
using GrupyzServices.Logging;


namespace PreBuildGrupyz
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            

           routes.MapRoute(
                "Checkout", // Route name
                "Checkout/{action}", // URL with parameters
                new { controller = "Checkout", action = "Checkout" } // Parameter defaults
            );

           routes.MapRoute(
                "CheckoutOK", // Route name
                "CheckoutOK/{action}", // URL with parameters
                new { controller = "Checkout", action = "CheckoutOK" } // Parameter defaults
            );

           routes.MapRoute(
            "CheckoutError", // Route name
            "CheckoutError/{action}", // URL with parameters
            new { controller = "Checkout", action = "CheckoutError" } // Parameter defaults
             );

           routes.MapRoute(
              "RPX.Signout", // Route name
              "RPXAuthentication/SignOut", // URL with parameters
              new { controller = "RPXAuthentication", action = "SignOut" } // Parameter defaults
              );

           routes.MapRoute(
               "RPX.HandleResponse", // Route name
               "RPXAuthentication/HandleResponse", // URL with parameters
               new { controller = "RPXAuthentication", action = "HandleResponse" } // Parameter defaults
               );

           routes.MapRoute(
                "Deal", // Route name
                "Deal/{id}", // URL with parameters
                new { controller = "Deal", action = "Detalhe" },
                new { id = new DealRouteConstraint() }    
             );

           routes.MapRoute(
                "Referencia", // Route name
                "Referencia/{id}", // URL with parameters
                new { controller = "Home", action = "Referencia" },
                new { id = UrlParameter.Optional }
             );

           routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
           );
            
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            
            RegisterRoutes(RouteTable.Routes);
        }


        protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();
            
            // Log the exception.
            ILogger logger = new NLogLogger();
            logger.Error(exception);

            Response.Clear();

            HttpException httpException = exception as HttpException;
            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", "Error");

            if (httpException == null)
            {
                routeData.Values.Add("action", "Index");
            }
            else //It's an Http Exception, Let's handle it.
            {

                switch (httpException.GetHttpCode())
                {
                    case 404:
                        // Page not found.
                        routeData.Values.Add("action", "HttpError404");
                        break;
                    case 500:
                        // Server error.
                        routeData.Values.Add("action", "HttpError500");
                        break;
                    // Here you can handle Views to other error codes.
                    // I choose a General error template  
                    default:
                        routeData.Values.Add("action", "General");
                        break;
                }
            }

            // Pass exception details to the target error View.
            routeData.Values.Add("error", exception);

            // Clear the error on server.
            Server.ClearError();

            // Call target Controller and pass the routeData.
            IController errorController = new ErrorController();
            errorController.Execute(new RequestContext(
                new HttpContextWrapper(Context), routeData));

        }


    }
}